﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using Basic4Net.BusinessRules;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;

namespace Basic4Net.OAWeb.Module_Finance
{
    public partial class Main : BasePage
    {
        private static Amount _amount;
        protected static Amount HAmount
        {
            get { return _amount ?? (_amount = new Amount()); }
        }

        private IList<t_Amount> _currentAmount;

// ReSharper disable InconsistentNaming
        protected void Page_Load(object sender, EventArgs e)
// ReSharper restore InconsistentNaming
        {
            Bind();
        }

        protected void BtnOkClick(object sender, EventArgs e)
        {
            bool result = true;
            double money;
            if (!Double.TryParse(txtMoney.Text, out money))
            {
               // result = false;
                //update by lxy 20100916 添加输入为空的判断
                txtMsg.Text = "您还未输入拨款金额";
                return;
            } 
            int appType;
            if (!Int32.TryParse(drType.SelectedValue, out appType))
            {
               // result = false;
                //update by lxy 20100916 添加输入拨款类型异常的判断 
                txtMsg.Text = "请您重新选择拨款类型";
                return;
            }
            if(_currentAmount != null && _currentAmount.Count > 0 && result)
                if (_currentAmount[0].FYear.HasValue && _currentAmount[0].FMonth.HasValue)
                    result = HAmount.UpdateAmount(appType, _currentAmount[0].FYear.Value, _currentAmount[0].FMonth.Value, money, SchoolMark);

            Bind();
            if (!result)
            {
                
                txtMsg.Text = "金额输入错误";
                txtMsg.Style[HtmlTextWriterStyle.Color] = "red";
            }
            else
            {
                txtMsg.Text = "";
            }

        }

        private void Bind()
        {
            _currentAmount = HAmount.GetAllActiveAmount(SchoolMark);
            if (_currentAmount == null || _currentAmount.Count < 1)
            {
                labCurrent.Text = "没有激活核算月, 请进入\"流程管理\"页面设置一个当前核算月.";
                labS.Visible = false;
                drType.Visible = false;
                btnOK.Visible = false;
                txtMoney.Visible = false;
                return;
            }
            if (_currentAmount[0].Verify.HasValue &&_currentAmount[0].Verify.Value == 1)
            {
                labCurrent.Text = "当前核算月已经经过审核, 不能再次进行拨款, 请进入\"流程管理\"页面设置下一个核算月.";
                labS.Visible = false;
                drType.Visible = false;
                btnOK.Visible = false;
                txtMoney.Visible = false;
                return;
            }

            labCurrent.Text = String.Format("我要将{0}年{1}月的", _currentAmount[0].FYear, _currentAmount[0].FMonth);

            double money1 = 0;
            double money2 = 0;
            double money3 = 0;
            foreach (t_Amount tAmount in _currentAmount)
            {
                if (tAmount.AppType == 1)
                {
                    if (tAmount.AppAmount != null)
                        money1 = tAmount.AppAmount.Value;
                }
                else if (tAmount.AppType == 2)
                {
                    if (tAmount.AppAmount != null)
                        money2 = tAmount.AppAmount.Value;
                }
                else if (tAmount.AppType == 3)
                {
                    if (tAmount.AppAmount != null)
                        money3 = tAmount.AppAmount.Value;
                }
            }

            labMsg.Text = String.Format("当前{0}年{1}月的财政拨款:{2}元, 自筹资金:{3}元, 临聘教师拨款:{4}元.",
                 _currentAmount[0].FYear, _currentAmount[0].FMonth, money1, money2, money3);
        }

        protected void drType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtMoney.Text = "";
        }
    }
}